package com.aysf.drafter.controller;

import com.aysf.drafter.entity.CustomerCheckIn;
import com.aysf.drafter.service.ICustomerCheckInService;
import com.aysf.drafter.vo.CheckOutExitVO;
import com.aysf.drafter.vo.CheckOutVO;
import com.aysf.drafter.entity.CustomerCheckOut;
import com.aysf.drafter.entity.Result;
import com.aysf.drafter.service.ICustomerCheckOutService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author MaoZz
 * @since 2024-12-21 17:18:55
 */
@RestController
@CrossOrigin
@RequestMapping("/customerCheckOut")
public class CustomerCheckOutController {

    @Autowired
    private ICustomerCheckOutService customerCheckOutService;
    @Autowired
    private ICustomerCheckInService customerCheckInService;



    @RequestMapping("/getall")
    public Result getall(Integer current,Integer size){
        Page<CustomerCheckOut> page = new Page<>(current, size);
        Page<CustomerCheckOut> page1 = customerCheckOutService.page(page);
        List<CheckOutVO> checkOutVOS = new ArrayList<>();
        for (CustomerCheckOut record : page1.getRecords()) {
            CheckOutVO checkOutVO = new CheckOutVO();
            checkOutVO.setCurrent(page1.getCurrent());
            checkOutVO.setSize((int) page1.getSize());
            checkOutVO.setTotal((int) page1.getTotal());
            CustomerCheckIn checkIn = customerCheckInService.getOne(new QueryWrapper<CustomerCheckIn>()
                    .eq("customer_id", record.getCustomerId()));
            checkOutVO.setName(checkIn.getName());
            checkOutVO.setAge(checkIn.getAge());
            checkOutVO.setGender(checkIn.getGender());
            checkOutVO.setIdCard(checkIn.getIdCard());
            checkOutVO.setPhone(checkIn.getPhone());
            checkOutVO.setCheckOutReason(record.getCheckOutReason());
            checkOutVO.setApplicationDate(record.getApplicationDate());
            checkOutVO.setCheckOutType(record.getCheckOutType());
            checkOutVO.setBirthDate(checkIn.getBirthDate());
            checkOutVO.setCheckInDate(checkIn.getCheckInDate());
            checkOutVO.setCheckOutDate(record.getCheckOutDate());
            checkOutVO.setStatus(record.getStatus());

            checkOutVOS.add(checkOutVO);
        }
        if(page1.getRecords().size()>0){
            return new Result("success",checkOutVOS);
        }
        else return new Result("error");
    }


    @RequestMapping("/exit")
    //todo:管理员层面还得整一个床位管理相关的，因为只有这样才能床位持恒
    public Result exit(@RequestBody CheckOutExitVO checkOutExitVO) {
        System.out.println(checkOutExitVO);
        QueryWrapper<CustomerCheckIn> wrapper = new QueryWrapper<CustomerCheckIn>().eq("name", checkOutExitVO.getName())
                .eq("id_card", checkOutExitVO.getIdCard());
        CustomerCheckIn checkIn = customerCheckInService.getOne(wrapper);
        //目的是得到id,但是只有姓名
        CustomerCheckOut customerCheckOut = new CustomerCheckOut();
        customerCheckOut.setCustomerId(checkIn.getCustomerId());
        customerCheckOut.setCheckOutReason(checkOutExitVO.getCheckOutReason());
        customerCheckOut.setCheckOutDate(checkOutExitVO.getCheckOutDate());
        customerCheckOut.setCheckOutType(checkOutExitVO.getCheckOutType());
        customerCheckOut.setApplicationDate(checkOutExitVO.getApplicationDate());
        boolean save = customerCheckOutService.save(customerCheckOut);
        if(save){
            return new Result("success");
        }
        else return new Result("error");

    }

}
